package com.ltx.blog_ltx.controller;


import com.alibaba.fastjson.JSONObject;
import com.ltx.blog_ltx.entity.dto.Friendendcontent;
import com.ltx.blog_ltx.entity.dto.OneFriendContent;
import com.ltx.blog_ltx.mapper.ChatMapper;
import com.ltx.blog_ltx.mapper.UnreadNumMapper;
import com.ltx.blog_ltx.mapper.UserMapper;
import com.ltx.blog_ltx.mapper.attentionMapper;
import com.ltx.blog_ltx.until.EmojiUntil;
import com.ltx.blog_ltx.until.jwtUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class ChatController {

    @Autowired
    private attentionMapper attentionMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ChatMapper chatMapper;
    @Autowired
    private UnreadNumMapper unreadNumMapper;

    @PostMapping("/GetfriendInfo")
    public List<Map<String,String>> GetfriendInfo(@RequestBody JSONObject param){
        String user_token = param.getString("user_token");
        String tokenName = jwtUntil.getTokenName(user_token);
        int userId = userMapper.queryIdByName(tokenName);
        List<Integer> integers = attentionMapper.AllAttention(userId);
        List<String> strings = userMapper.attentionName(integers);
        List<String> strings1 = userMapper.attentionHead(integers);
        ArrayList<Map<String,String>> objects = new ArrayList<>();
        List<Friendendcontent> friendendcontents = chatMapper.endContent(tokenName);
        for(int i=0;i<integers.size();i++){
            Map<String,String> map=new HashMap<>();
            map.put("userName",strings.get(i));
            System.out.println(strings.get(i));
            map.put("userHead",strings1.get(i));
            Integer unreadNum = unreadNumMapper.friendUnreadNum(strings.get(i), tokenName);
            map.put("unreadNum", String.valueOf(unreadNum));
            List<OneFriendContent> onefriendcontent = chatMapper.onefriendcontent(tokenName, strings.get(i));
            if(onefriendcontent.size()!=0){
                Collections.sort(onefriendcontent);
                map.put("end_content", onefriendcontent.get(onefriendcontent.size()-1).getChatContent());
            }
            objects.add(map);
        }
        return objects;
    }

    @PostMapping("/ChatOneContent")
    private List<OneFriendContent> ChatOneContent(@RequestBody JSONObject param){
        String username = param.getString("username");
        String friendName = param.getString("friendName");
        List<OneFriendContent> onefriendcontent = chatMapper.onefriendcontent(username, friendName);
        for(int i=0;i<onefriendcontent.size();i++){
            String chatContent = onefriendcontent.get(i).getChatContent();
            if(EmojiUntil.containsEmoji(chatContent)){
                onefriendcontent.get(i).setChatContent(EmojiUntil.emojiConverterUnicodeStr(chatContent));
            }
        }
        Collections.sort(onefriendcontent);
        return onefriendcontent;
    }

    @PostMapping("/readMessage")
    private void readMessage(@RequestBody JSONObject param){
        String username = param.getString("username");
        String friendName = param.getString("friendName");
         unreadNumMapper.readMessage(friendName,username);
    }

    @PostMapping("/addMessageNum")
    private  void addMessageNum(@RequestBody JSONObject param){
        String username = param.getString("username");
        String friendName = param.getString("friendName");
        unreadNumMapper.addMessageNum(friendName,username);
    }


}
